/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.dataTypePropGen.assoUdt;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.designtime.api.collection.GspFieldCollection;
import com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.DataTypeDataGeneratorContext;
import com.inspur.edp.cef.generator.entityGenerator.base.elements.ElementAssoGenerator;
import com.inspur.edp.cef.generator.overall.CefGeneratorContext;
import com.inspur.edp.udt.designtime.api.entity.SimpleDataTypeDef;
import lombok.var;
import org.eclipse.jdt.core.dom.*;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;

public class AssoUdtEnrichedInfoCtorGenerator extends ClassMethodGenerator {
    private SimpleDataTypeDef udt;
    private DataTypeDataGeneratorContext dataGenContext;
    private String className;

    public AssoUdtEnrichedInfoCtorGenerator(
            SimpleDataTypeDef udt, String className, DataTypeDataGeneratorContext dataGenContext) {

        this.className = className;
        this.udt = udt;
        this.dataGenContext = dataGenContext;
    }

    @Override
    protected String getMethodName() {
        return className;
    }

    @Override
    protected TypeInfo getReturnType() {
        return null;
    }

    @Override
    protected boolean buildIsConstructor() {
        return true;
    }

    @Override
    protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
        return new ArrayList<Modifier.ModifierKeyword>(
                Arrays.asList(new Modifier.ModifierKeyword[]{Modifier.ModifierKeyword.PUBLIC_KEYWORD}));
    }

    private static final String parName = "inner";

    @Override
    protected ArrayList<ParameterInfo> getParameterCollection() {
        String packageName =
                udt.getEntityAssemblyInfo().getAssemblyName();
        String className = udt.getContainElements().get(0).getLabelID()
                + ElementAssoGenerator.nameSuffix;
        CefGeneratorContext cefContext =
                dataGenContext.getDataTypeGeneratorContext().getEntityGenContext().getCefGenContext();
        ParameterInfo param = new ParameterInfo();
        param.setParamName(parName);
        TypeInfo typeInfo = new TypeInfo(className);
        typeInfo.setTypePackageName(packageName);
        param.setParamType(typeInfo);
        return new ArrayList<ParameterInfo>(
                Arrays.asList(new ParameterInfo[]{param}));

    }

    // region methodBody

    @Override
    protected Block buildMethodBody() {

        Block block = ast.newBlock();

        IfStatement ifStatement = ast.newIfStatement();
        InfixExpression inifExpression = ast.newInfixExpression();
        inifExpression.setLeftOperand(ast.newSimpleName(parName));
        inifExpression.setOperator(InfixExpression.Operator.EQUALS);
        inifExpression.setRightOperand(ast.newNullLiteral());

        ifStatement.setExpression(inifExpression);
        ReturnStatement returnStatement = ast.newReturnStatement();
        ifStatement.setThenStatement(returnStatement);

        block.statements().add(ifStatement);

        block.statements().addAll(getBasePropStatements());
        return block;
    }

    private ArrayList<Statement> getBasePropStatements() {
        CefGeneratorContext cefContext =
                dataGenContext.getDataTypeGeneratorContext().getEntityGenContext().getCefGenContext();

        String udtInfoAssembly = udt.getEntityAssemblyInfo().getAssemblyName();

        String udtInfoClassName =
                udt.getContainElements().get(0).getLabelID() +
                        ElementAssoGenerator.nameSuffix;


        GspFieldCollection refElements = udt.getChildAssociations().get(0).getRefElementCollection();
        ArrayList<Statement> rez = new ArrayList<Statement>(refElements.size() + 1);
        for (int i = 0; i < refElements.size(); i++) {

            MethodInvocation invocation = ast.newMethodInvocation();
            invocation.setName(ast.newSimpleName("set" + refElements.get(i).getLabelID()));

            MethodInvocation arg = ast.newMethodInvocation();
            arg.setExpression(ast.newSimpleName(parName));
            arg.setName(ast.newSimpleName("get" + refElements.get(i).getLabelID()));

            invocation.arguments().add(arg);
            rez.add(ast.newExpressionStatement(invocation));
        }
        MethodInvocation invocation = ast.newMethodInvocation();
        invocation.setName(ast.newSimpleName("set" + udt.getCode()));
        MethodInvocation arg = ast.newMethodInvocation();
        arg.setExpression(ast.newSimpleName(parName));
        arg.setName(ast.newSimpleName("get" + udt.getCode()));
        invocation.arguments().add(arg);
        rez.add(ast.newExpressionStatement(invocation));

        return rez;

    }
    // endregion
}
